package com.czapl.drawing;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.czapl.game3.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;

public class GameBoard extends TableLayout{

	private Paint p;
	private Rect sprite1Bounds = new Rect(0,0,0,0);
	private Rect sprite2Bounds = new Rect(0,0,0,0);
	private Point sprite1;
	private Point sprite2;
	private Bitmap bm1 = null;
	private Bitmap bm2 = null;
	private boolean collisionDetected = false;
	private Point lastCollision = new Point(-1,-1);
	private List<Tile> tileMap = null;

public static final int NUM_OF_ROWS = 6;
public static final int NUM_OF_COLS = 12;

public GameBoard(Context context, AttributeSet aSet) {
    super(context, aSet);
    p = new Paint();
    sprite1 = new Point(-1,-1);
    sprite2 = new Point(-1,-1);
    bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.asteroid);
    bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.ufo);
    sprite1Bounds = new Rect(0,0, bm1.getWidth(), bm1.getHeight());
    sprite2Bounds = new Rect(0,0, bm2.getWidth(), bm2.getHeight());
    //setWillNotDraw(false);
    //TableLayout.LayoutParams param1 = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    //param1.setMargins(20, 20, 20, 20);
    //setLayoutParams(param1);
    setGravity(Gravity.CENTER);
    //setStretchAllColumns(true);
    
    initializeTileMap(context);
}

public GameBoard(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs);
}


synchronized public void setSprite1(int x, int y) {
             sprite1=new Point(x,y);
       }

synchronized public int getSprite1X() {
             return sprite1.x;
       }

synchronized public int getSprite1Y() {
             return sprite1.y;
       }

synchronized public void setSprite2(int x, int y) {
             sprite2=new Point(x,y);
       }

synchronized public int getSprite2X() {
             return sprite2.x;
       }

synchronized public int getSprite2Y() {
             return sprite2.y;
       }


synchronized public int getSprite1Width() {
             return sprite1Bounds.width();
       }

synchronized public int getSprite1Height() {
             return sprite1Bounds.height();
       }

synchronized public int getSprite2Width() {
             return sprite2Bounds.width();
       }

synchronized public int getSprite2Height() {
             return sprite2Bounds.height();
       }

synchronized public Point getLastCollision() {
             return lastCollision;
       }

synchronized public boolean wasCollisionDetected() {
             return collisionDetected;
       }

synchronized private void initializeTileMap(Context context) {
    tileMap = new ArrayList<Tile>();
    for (int row=0; row<NUM_OF_ROWS; row++) {
    	TableRow tr = new TableRow(context);
    	for(int col=0; col<NUM_OF_COLS; col++) {
    		Tile tile = new Tile(context, "b"+Integer.toString(col));
    		tileMap.add(tile);
    		TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    		tr.setLayoutParams(params);
    		tr.setGravity(Gravity.CENTER);
    		tr.addView(tile);
    		//tr.addView(tile, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    	}
    	addView(tr);
    }
}

private boolean checkForCollision() {
             if (sprite1.x<0 && sprite2.x<0 && sprite1.y<0 && sprite2.y<0) return
false;
             Rect r1 = new Rect(sprite1.x, sprite1.y, sprite1.x +
sprite1Bounds.width(),  sprite1.y + sprite1Bounds.height());
             Rect r2 = new Rect(sprite2.x, sprite2.y, sprite2.x +
sprite2Bounds.width(), sprite2.y + sprite2Bounds.height());
             Rect r3 = new Rect(r1);
             if(r1.intersect(r2)) {
                    for (int i = r1.left; i<r1.right; i++) {
                           for (int j = r1.top; j<r1.bottom; j++) {
                                  if (bm1.getPixel(i-r3.left, j-r3.top)!=
Color.TRANSPARENT) {
                                        if (bm2.getPixel(i-r2.left, j-r2.top) !=
Color.TRANSPARENT) {
                                              lastCollision = new Point(sprite2.x +
i-r2.left, sprite2.y + j-r2.top);
                                              return true;
                                        }
                                  }
                            }
                     }
             }
             lastCollision = new Point(-1,-1);
             return false;
      }
//      @Override
//      synchronized public void dispatchDraw(Canvas canvas) {
//            p.setColor(Color.BLACK);
//            p.setAlpha(255);
//         p.setStrokeWidth(1);
//            canvas.drawRect(0, 0, getWidth(), getHeight(), p);
//
//if (starField==null) {
//                   initializeStars(canvas.getWidth(), canvas.getHeight());
//                   initializeTileMap(canvas);
//            }
//            p.setColor(Color.CYAN);
//            p.setAlpha(starAlpha+=starFade);
//            if (starAlpha>=252 || starAlpha <=80) starFade=starFade*-1;
//            p.setStrokeWidth(5);
//            for (int i=0; i<NUM_OF_STARS; i++) {
//                   canvas.drawPoint(starField.get(i).x, starField.get(i).y, p);
//            }
//            for (int i=0; i<NUM_OF_ROWS*NUM_OF_COLS; i++) {
//                //tileMap.get(i).draw(canvas);
//            }
//
//if (sprite1.x>=0) {
//                   m.reset();
//                   m.postTranslate((float)(sprite1.x), (float)(sprite1.y));
//                   m.postRotate(sprite1Rotation,
//(float)(sprite1.x+sprite1Bounds.width()/2.0),
//(float)(sprite1.y+sprite1Bounds.width()/2.0));
//                   canvas.drawBitmap(bm1, m, null);
//                   sprite1Rotation+=5;
//                   if (sprite1Rotation >= 360) sprite1Rotation=0;
//             }
//             if (sprite2.x>=0) {
//                    canvas.drawBitmap(bm2, sprite2.x, sprite2.y, null);
//             }
//             collisionDetected = checkForCollision();
//             if (collisionDetected ) {
//                     p.setColor(Color.RED);
//                     p.setAlpha(255);
//                 p.setStrokeWidth(5);
//                 canvas.drawLine(lastCollision.x - 5, lastCollision.y - 5,
//lastCollision.x + 5, lastCollision.y + 5, p);
//                 canvas.drawLine(lastCollision.x + 5, lastCollision.y - 5,
//lastCollision.x - 5, lastCollision.y + 5, p);
//              }
//       }

private void drawForeground(Canvas canvas) {
	p.setColor(Color.rgb(200, 100, 50));
	p.setStrokeWidth(1);
	canvas.drawCircle(getSprite1X(), getSprite1Y(), 20, p);
	p.setColor(Color.rgb(50, 50, 250));
	canvas.drawCircle(getSprite2X(), getSprite2Y(), 20, p);
}

@Override
protected void dispatchDraw(Canvas canvas) {
	super.dispatchDraw(canvas);
	drawForeground(canvas);
}
}

